﻿    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ADOX;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;
    using System.ComponentModel;
    using System.Drawing;
using Excel;


namespace TemplateGenerator.Controls
{
        public class ImportExcel
        {
            /// <summary>
            /// Import a table from an excel file.
            /// </summary>
            /// <param name="file">
            /// Name of the file (path and name)
            /// </param>
            /// <param name="tabData">
            /// Name of the desired table
            /// </param>
            /// <returns>
            /// Table
            /// </returns>
            public DataSet Import(string file)
            {

                if (file.Trim() != string.Empty)
                {
                    try
                    {
                        return GetDataTableExcel(file);

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }

                }

                return null;
            }

            /// <summary>
            /// Retrieve the data inside the table.
            /// </summary>
            /// <param name="strFileName">
            /// Name of the file (path and name)
            /// </param>
            /// <returns>
            /// Returns a DataSet structure.
            /// </returns>
            public static DataSet GetDataTableExcel(string strFileName)
            {
                FileStream stream = File.Open(strFileName, FileMode.Open, FileAccess.Read);

                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

                excelReader.IsFirstRowAsColumnNames = true;
                return excelReader.AsDataSet();

            }

        }
    }


